package com.mbuswidget;

import android.app.TabActivity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class MBusWidgetConfiguration extends TabActivity {
	private Integer widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	public MBusWidgetConfiguration() {
		super();
	}

	

	//
	// void doBindService() {
	// // Establish a connection with the service. We use an explicit
	// // class name because we want a specific service implementation that
	// // we know will be running in our own process (and thus won't be
	// // supporting component replacement by other applications).
	// bindService(
	// new Intent(MBusWidgetConfiguration.this, MBusWidgetService.class),
	// mConnection, Context.BIND_AUTO_CREATE);
	// mIsBound = true;
	// }
	//
	// void doUnbindService() {
	// if (mIsBound) {
	// // Detach our existing connection.
	// unbindService(mConnection);
	// mIsBound = false;
	// }
	// }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widgetconfig);
		setResult(RESULT_CANCELED);

		widgetID = getIntent().getIntExtra(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		if(widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}

		// // If they gave us an intent without the widget id, just bail.
		// if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
		// finish();
		// }

		//Log.w("mbuswidget", "Editing widget with id: " + widgetID);

		

		// startService(new Intent(MBusWidgetActivity.this,
		// MBusWidgetService.class));

		// Start up the service so we can get bus information for later...
		// doBindService();

		// Name Tabs

		Resources res = getResources();
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(MBusWidgetConfiguration.this,
				MBusWidgetConfigurationStops.class);
		
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
		
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("Stops").setIndicator("Stops", res.getDrawable(R.drawable.ic_tab_stops))
				.setContent(intent);
		
		tabHost.addTab(spec);
		
		
		intent = new Intent().setClass(MBusWidgetConfiguration.this, MBusWidgetConfigurationInfo.class);
		spec = tabHost.newTabSpec("Route Abbreviations").setIndicator("Route Abbreviations", res.getDrawable(R.drawable.ic_tab_info)).setContent(intent);
		tabHost.addTab(spec);
		
		
		intent = new Intent().setClass(this,
				MBusWidgetConfigurationSettings.class);
		
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);

		spec = tabHost.newTabSpec("Settings").setIndicator("Settings", res.getDrawable(R.drawable.ic_tab_settings))
				.setContent(intent);

		tabHost.addTab(spec);

	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Intent intent = new Intent(MBusWidgetConfiguration.this, MBusWidgetService.class);
		startService(intent);
	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// super.onActivityResult(requestCode, resultCode, data);
	// switch (requestCode) {
	// case (0): {
	// if (resultCode == Activity.RESULT_OK) {
	// // We have information required. Complete Widget setup.
	//
	// final Context context = MBusWidgetConfiguration.this;
	//
	// String stop = data.getExtras().getString("stop");
	//
	// SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME,
	// 0).edit();
	// prefs.putInt(PREF_PREFIX_KEY + widgetID + "r", route);
	// prefs.putString(PREF_PREFIX_KEY + widgetID + "s", stop);
	// prefs.commit();
	//
	// AppWidgetManager appWidgetManager = AppWidgetManager
	// .getInstance(context);
	// RemoteViews views = new RemoteViews(context.getPackageName(),
	// R.layout.mbuswidget);
	// appWidgetManager.updateAppWidget(widgetID, views);
	//
	// Intent intent = new Intent();
	// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
	// setResult(RESULT_OK, intent);
	// // doUnbindService();
	// finish();
	// }
	// break;
	// }
	// }
	// }
}
